<?php
declare (strict_types=1);

namespace app\api\controller\store;

use app\admin\model\store\GoodsSku;
use app\api\controller\Base;
use app\common\service\store\BargainService;
use app\common\service\store\GoodsService;
use app\common\service\store\RecommendService;
use think\facade\Event;
use think\Request;

class Goods extends Base
{
    /**
     * Author:Yingbo Sun
     * 获取模块名称
     * @return \think\response\Json
     */
    public function commend()
    {
        $data = RecommendService::getCommend();

        return json($data);
    }

    /**
     * 商品详情页
     * Author:Yingbo Sun
     * @return mixed|void
     */
    public function getGoodsDetail()
    {
        if (!$this->request->isGet()) {
            return $this->result([], 400, '请求方式错误');
        }

        $goods_id = $this->request->get('goods_id', 0, 'int');
        if (empty($goods_id) || $goods_id < 1) {
            return $this->result([], 400, '参数错误');
        }

        $data = GoodsService::getGoodsDetail($goods_id, $this->request->uid);

        return $this->res($data);
    }

    /**
     * 获取模块化商品
     */
    public function commendList()
    {
        $label = $this->request->get('label');
        $data = RecommendService::getCommendGoods($label);
        return $this->res($data);
    }

    /**
     * Author:Yingbo Sun
     * 热门推荐
     */
    public function hotRecommend()
    {
        $data = GoodsService::getHotRecommend();
        return $this->res($data);
    }

    /**
     * Author:YangXinPeng
     * 商品搜索
     * @param Request $request
     */
    public function search(Request $request)
    {
        $keyword = $request->get('keyword','','trim');
        if ($keyword == ''){
            return $this->apiFail('搜索参数错误');
        }
        if (strlen($keyword) == 9) {
            $bargainUser = BargainService::getBargainUser($keyword);
            if ($bargainUser['code'] == 200) {
                return $this->apiSuccess('跳转帮砍页面',$bargainUser['data'],302);
            }
        }
        $res = GoodsService::search($keyword,$this->request->uid);
        return $this->apiSuccess('成功',$res);
    }

    /**
     * Author:Yingbo Sun
     * 分类商品列表
     * @return mixed
     */
    public function cateGoodsList()
    {
        $cateId = $this->request->get('cate_id');
        if (empty($cateId)){
            $data = ['code' => 400,'message' => '参数错误'];
            return $this->res($data);
        }
        $data = GoodsService::cateGoodsList($cateId);
        return $this->res($data);
    }

    //更新销量
    public function liveGoodsNumber()
    {
        //购买件数
        $num = $this->request->post('number',0);
        $goodsId = $this->request->post('goods_id',0);
        if (empty($num) || empty($goodsId)){
            $data = ['code' => 400,'message' => '参数错误'];
            return $this->res($data);
        }

        GoodsService::liveGoodsNumberUp($num,$goodsId);
    }


    public function getSkuById(Request $request)
    {
        $skuId = $request->get('sku_id');
        $goodsId = GoodsSku::where('id',$skuId)->value('goods_id');
        $data = \app\admin\model\store\Goods::where('id',$goodsId)->with('sku')->select();
        $data && $data = $data->toArray();
        foreach ($data as $k=>$value){
            foreach ($value['sku'] as $sku){
                $str = '';
               $arr = json_decode( $sku['attr_values'],true);
               foreach ($arr as $v){
                   $str.=$v;
               }
                $sku['attr_values'] = $str;
                if ($sku['id'] == $skuId) {
                    $data[$k]['skuu'] = $sku;
                }
            }
            unset($data[$k]['sku']);
        }
        return json(['code'=>200,'msg'=>'成功','data'=>$data]);
    }


    public function getGood(Request $request)
    {
        $sku_id = $request->get('sku_id');
        $goods_id = GoodsSku::where('id',$sku_id)->value('goods_id');
        $uid = $request->uid;
        $data = GoodsService::getGoodDetail($goods_id,$sku_id,$uid);
        return $this->res($data);
    }
}
